using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame
{

    /// <summary>
    /// 方向盘组件
    /// </summary>
    public class KFSteeringwheel : MonoBehaviour
    {
        /// <summary>
        /// 输出的数据结构
        /// </summary>
        public struct SteeringWheelData
        {
            //进度值
            public float progress;
            //方向值
            public float directionValue;
            //实制值
            public double value;
            //影射值
            public float rangeShadow;

        }

        /// <summary>
        /// 影射数据结构
        /// </summary>
        public struct RangeData
        {
            //影射值
            public float value;
            //范围最小 progress值
            public float minvalue;
            //范围最大 progress值
            public float maxvalue;
        }


        /// <summary>
        /// 影射表
        /// </summary>
        /// <typeparam name="RangeData"></typeparam>
        /// <returns></returns>
        public List<RangeData> shadows = new List<RangeData>();

        //最大值 右边
        public int maxDirectionValue = 255;
        //最小值 左边
        public int minDirectionValue = 0;
        //中间值
        public int middleDirectionValue = 125;
        /// <summary>
        /// 取值进度 0--1;
        /// </summary>
        public float progress = 0.5f;

        /// <summary>
        /// 输入的值
        /// </summary>
        public double value = 0;

        /// <summary>
        /// 是否加载配置
        /// </summary>
        public bool loadConfig = true;

        /// <summary>
        /// 通过事件返回
        /// </summary>
        public delegate void OnSteeringWheel(SteeringWheelData data);
        OnSteeringWheel onSteeringWheel;
        /// <summary>
        /// 添加侦听器
        /// </summary>
        /// <param name="wheel"></param>
        public void addLisener(OnSteeringWheel wheel)
        {
            onSteeringWheel += wheel;
        }
        /// <summary>
        /// 移除侦听器
        /// </summary>
        /// <param name="wheel"></param>
        public void removeLisener(OnSteeringWheel wheel)
        {
            onSteeringWheel -= wheel;
        }


        /// <summary>
        /// 区分左右 使用正负
        /// </summary>
        /// <returns></returns>
        public static float LeftRingProgress(float pro)
        {
            return (pro - 0.5f) / 0.5f;
        }


        /// <summary>
        /// 共享对象使用
        /// </summary>
        static KFSteeringwheel steeringwheel;
        public static KFSteeringwheel Instantiation()
        {
            if (!steeringwheel)
            {
                steeringwheel = new KFSteeringwheel();
                steeringwheel.load();
            }
            return steeringwheel;

        }

        public void Start()
        {
            load();
        }

        /// <summary>
        /// 加载
        /// /// </summary>
        public void load()
        {
            if (!loadConfig)
                return;

            loadconfig();
            loadRangConfig();
        }

        /// <summary>
        /// 加载配置表
        /// </summary>
        public void loadconfig()
        {
            INIParser configp = new INIParser();
            string path = "KFConfig/kf_steeringwheel";
            string group = "KFINFO";
            TextAsset ta = Resources.Load(path) as TextAsset;
            if (ta)
                configp.Open(ta);

            minDirectionValue = configp.ReadValue(group, "minvalue", 0);
            maxDirectionValue = configp.ReadValue(group, "maxvalue", 255);
            middleDirectionValue = configp.ReadValue(group, "midvalue", 0);

            if (middleDirectionValue == 0)
                middleDirectionValue = (maxDirectionValue - minDirectionValue) / 2;
        }

        /// <summary>
        /// 加载范围表
        /// </summary>
        public void loadRangConfig()
        {
            INIParser configp = new INIParser();
            string path = "KFConfig/kf_steeringwheel_ranges";
            TextAsset ta = Resources.Load(path) as TextAsset;
            if (ta)
                configp.Open(ta);

            var items = configp.SectionNames();
            foreach (var item in items)
            {
                RangeData data = new RangeData();
                data.value = configp.ReadValue(item, "value", 0);
                data.maxvalue = configp.ReadValue(item, "maxvalue", 0);
                data.minvalue = configp.ReadValue(item, "minvalue", 0);
                shadows.Add(data);
            }
        }

        /// <summary>
        /// 抛出数据
        /// </summary>
        void popDatas()
        {
            if (onSteeringWheel != null)
            {
                SteeringWheelData data = new SteeringWheelData();
                data.progress = progress;
                data.directionValue = LeftRingProgress(progress);
                data.rangeShadow = valueShadows(progress);
                data.value = value;
                onSteeringWheel(data);
            }
        }

        /// <summary>
        /// 设置左方向值  -1 -- 1
        /// </summary>
        /// <param name="f"></param>
        public void setDirectionValue(float f)
        {
            progress = f * 0.5f + 0.5f;

            popDatas();
        }

        /// <summary>
        /// 设置进度值 0--1
        /// </summary>
        /// <param name="p"></param>
        public void setProgress(float p)
        {
            progress = p;
            popDatas();
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="number"></param>
        public void setValue(int number)
        {
            value = number;
            progress = (number - minDirectionValue) / (maxDirectionValue - middleDirectionValue);
            popDatas();
        }

        /// <summary>
        /// 设置一个数值得到进度值
        /// </summary>
        /// <param name="value"></param>
        /// <param name="maxValie"></param>
        /// <param name="minValue"></param>
        public void setNumber(double value, double maxValie, double minValue = 0)
        {
            this.value = value;
            var b = (value - minValue) / (maxValie - minValue);
            progress = (float)b;
            popDatas();
        }


        /// <summary>
        /// 进度找取影射值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public float valueShadows(float value)
        {
            foreach (var item in shadows)
            {
                if (item.minvalue <= value && value <= item.maxvalue)
                    return item.value;
            }
            return 0;
        }
    }
}